import superagent from 'superagent';
import cheerio from 'cheerio';
import path from 'path';
import { asBlob } from 'html-docx-js-typescript';
import fs from 'fs';
import moment from 'moment';
import log from 'electron-log';
const isDevelopment = process.env.NODE_ENV !== 'production'
log.transports.file.resolvePath = () => path.join(__dirname, (isDevelopment?'':'../../') + 'logs/main.log');

interface article{
    link: string;
    title: string; 
    timestamp: number;
}
interface link {
    link: string;
    title: string;
    filePath: string;
}
export const getIndex = async function(): Promise<article[]>{
    let res;
    try {
        res = await superagent.get('https://m.gelonghui.com');
    } catch (error) {
        log.error(`getIndex error ${error}`);
        return [];
    }
    const $ = cheerio.load(res.text), articleList:article[] = [];
    const data = JSON.parse($('#__NEXT_DATA__').html() || '{}');
    const articles = data.props.pageProps.articles
    articles.forEach((article: { contents: article; }) => {
        articleList.push(article.contents);
    });
    return articleList;
}

export const getLink = async(link: string, filename: string, date: Date) => {
    let res;
    try {
        res = await superagent.get(link);
    } catch (error) {
        log.error(`getLink error ${error}`);
        return false;
    }
    const _date = moment(date).format('YYYY-MM-DD');
    const dirname = path.join(__dirname, (isDevelopment?'':'../../') + _date);
    const isExist = fs.existsSync(dirname);
    if(!isExist) {
        fs.mkdirSync(dirname);
    }
    const $ = cheerio.load(res.text);
    $('.article-relatedInfos').remove();
    $('#about-article').remove();
    const articleTitle: string ="<h1>" + $('.article-title').html() + "</h1>";
    const desc = "<p>" +$('.article-description').html() + "</p>";
    const summary = "<p>" +$('.article-summary').html() + "</p>";
    const articleContent = $('.article-with-html').html();
    const article = articleTitle + desc + summary + articleContent;
    let data: Buffer | Blob = await asBlob(article);
    if(Buffer.isBuffer(data)) {
        try {
            let filePath =  path.join(dirname, filename+'.docx');
            fs.writeFileSync(filePath, data)
            return filePath;
        } catch (error) {
            return false;
        }
    }else {
        return false;
    }
    // const $ = cheerio.load(res.text), articleList = []
    // let data = JSON.parse($('#__NEXT_DATA__').html());
    // let articles = data.props.pageProps.articles
    // articles.forEach(article => {
    //     articleList.push(article.contents);
    // });
    // return articles;
}
export const getData =async function() {
    const articleList:article[] = await getIndex(),  list:link[] = [];
    for (let index = 0; index < articleList.length; index++) {
        const article = articleList[index];
        const { link, title, timestamp} = article;
        const date: Date = new Date(timestamp*1000);
        if(link) {
            try {
                let filePath = await getLink(link, title.substr(0, 20), date);
                if(filePath) {
                    list.push({
                        title: title.substr(0, 20),
                        link,
                        filePath
                    });
                }
            } catch (error) {
                log.error(error);
            }
            
        }
    }
    return list;
 }